<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	function message($msg,$level){
		if(WIN){
			messageWindows($msg,$level);
		}
		else{
			$maxWidth = MAX_SCREEN_WIDTH;
			$out = "";
			switch($level){
				case "WARNING":
					$out .= chr(27) . "[42;43m [" . date('Y-m-d H:i:s') . " WARNING] ";
				break;
				case "ERROR":
					$out .= chr(27) . "[42;41m [" . date('Y-m-d H:i:s') . " ERROR  ] ";
				break;
				case "NOTICE":
					$out .= chr(27) . "[42;44m [" . date('Y-m-d H:i:s') . " NOTICE ] ";
				break;
				case "COMMENT":
					$out .= chr(27) . "[42;42m [" . date('Y-m-d H:i:s') . " COMMENT] ";
				break;
			}
			if(strlen($msg) > $maxWidth){
				$a_msg = str_split($msg,$maxWidth-39);
				$first = false;
				foreach($a_msg as $v){
					if(!$first){
						echo $out . $v . @str_repeat(" ",round($maxWidth-(strlen($out)+strlen($v)))) . chr(27) . "[0m\n";
						$first = true;
					}
					else{
						echo substr($out, 0, 8) . str_repeat(" ",31) . $v . @str_repeat(" ",round($maxWidth-(strlen($out)+strlen($v)))) . chr(27) . "[0m\n";
					}
				}
			}
			else{
				echo $out . $msg . @str_repeat(" ",round($maxWidth-(strlen($out)+strlen($msg)))) . chr(27) . "[0m\n";
			}
		}
	}
	function messageWindows($msg,$level){
		$out = "";
		switch($level){
			case "WARNING":
				$out .= "[" . date('Y-m-d H:i:s') . " WARNING] ";
			break;
			case "ERROR":
				$out .= "[" . date('Y-m-d H:i:s') . " ERROR  ] ";
			break;
			case "NOTICE":
				$out .= "[" . date('Y-m-d H:i:s') . " NOTICE ] ";
			break;
			case "COMMENT":
				$out .= "[" . date('Y-m-d H:i:s') . " COMMENT] ";
			break;
		}
		echo $out . $msg . "\n";
	}
?>
